import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.util.LinkedList;


/*
 * A Simple Keyboard test.dIsplays keys pressed and released to the screen
 */

public class KeyTest extends GameCore implements KeyListener {

	public static void main(String[] args) {
		new KeyTest().run();
	}
	private LinkedList messages = new LinkedList();
	
	public void init()
	{
		super.init();
		Window window = screen.getFullScreenWindow();
		//allow input of tab and others jetm ysed focus tranversal
		window.setFocusTraversalKeysEnabled(false);
		//register the object as a key listener for the window
		window.addKeyListener(this);
		
		addMessage("KeyInputTest. Press Escape to exit");
		
	}
	
	//a methood from the KeyListenner interface
	public void keyPressed(KeyEvent e)
	{
		int keyCode = e.getKeyCode();
		//exit the program if esc is press
		if (keyCode == KeyEvent.VK_ESCAPE)
		{
			stop();
		}
		else
		{
			addMessage("Pressed: " + KeyEvent.getKeyText(keyCode));
			
			//makesure the key ins not preocessed for anuthing else
			e.consume();
		}
	
	
	}
	
	//a method from the keyListenner interface
	public void keyReleased(KeyEvent e)
	{
		int keyCode = e.getKeyCode();
		addMessage("Released: " + KeyEvent.getKeyText(keyCode));
		//makesure the key ins not preocessed for anuthing else
		e.consume();
	}
	
	public void keyTyped(KeyEvent e)
	{
		e.consume();
	}
	
	//add a message to the list of
	
	public synchronized void addMessage(String message)
	{
		messages.add(message);
		if (messages.size()>= screen.getHeight() / FONT_SIZE)
		{
			messages.remove();
		}
	}
	
	//draw the list of msgs
	
	public synchronized void draw(Graphics2D g)
	{
		
		Window window = screen.getFullScreenWindow();
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		
		//draw bkg
		g.setColor(Color.white);
		g.fillRect(0, 0,screen.getWidth(),screen.getHeight());
		
		//draw messages
		
		g.setColor(window.getForeground());
		int y = FONT_SIZE;
		for (int i=0 ; i<messages.size(); i++)
		{
			g.drawString((String)messages.get(i), 5, y);
			y+= FONT_SIZE;
		}
		
		
	}
	
	
	
}
